﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class admin_edcategory : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("select * from category where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;
        SqlParameter paramid = new SqlParameter("@id", SqlDbType.BigInt);
        paramid.Value = Request.Params["id"];
        mySqlSelect.Parameters.Add(paramid);
        try
        {
            SqlDataReader rd = mySqlSelect.ExecuteReader();
            if (rd.HasRows)
            {
                rd.Read();
                isPro.Checked = rd.GetSqlBoolean(2).IsTrue;
                des.Text = rd.GetSqlString(3).Value;
            }
            rd.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            Console.WriteLine("Loi setValueFromDatabaseToForm : " + ex);
        }
    }

    protected void update_Click(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();

        SqlCommand mySqlSelect = new SqlCommand("update category set imageUrl = @url, isProduct = @pro, description = @des where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;

        SqlParameter desc = new SqlParameter("@des", SqlDbType.NVarChar, 150);
        desc.Value = des.Text;

        SqlParameter id = new SqlParameter("@id", SqlDbType.BigInt);
        id.Value = Request.QueryString["id"];

        SqlParameter pro = new SqlParameter("@pro", SqlDbType.Bit);
        if (isPro.Checked)
        {
            pro.Value = 1;
        }
        else
        {
            pro.Value = 0;
        }

        SqlParameter url = new SqlParameter("@url", SqlDbType.NVarChar, 100);

        String regex = "";
        if (img.PostedFile.ContentLength > 2097151)
        {
            info.Visible = true;
            info.Text = "Tập tin hình ảnh phải có dung lượng nhỏ hơn 2MB!";
            return;
        }
        else
        {
            regex = System.IO.Path.GetExtension(img.PostedFile.FileName.ToLower().ToString());
            String picPath = "~/admin/resource/images/" + img.PostedFile.FileName;
            if (regex == ".bmp" || regex == ".jpg" || regex == ".gif" || regex == ".png")
            {
                img.SaveAs(Server.MapPath(picPath));
            }
            else
            {
                info.Visible = true;
                info.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
            }
        }
        if (regex == "")
        {
            mySqlSelect = new SqlCommand("update category set isProduct = @pro, description = @des where id = @id", cn);
            mySqlSelect.Parameters.Add(desc);
            mySqlSelect.Parameters.Add(id);
            mySqlSelect.Parameters.Add(pro);
        }
        else
        {
            url.Value = img.PostedFile.FileName;
            mySqlSelect.Parameters.Add(desc);
            mySqlSelect.Parameters.Add(id);
            mySqlSelect.Parameters.Add(pro);
            mySqlSelect.Parameters.Add(url);
        }
        mySqlSelect.ExecuteNonQuery();
        cn.Close();
    }
}